﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.Calendar.RecurrenceRule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindStyle.SPCalendar
{
    public class EndOption
    {
        public EndType Type { get; set; }
        public int Instances { get; set; }
        public DateTime Date { get; set; }

        public EndOption()
        {
            this.Type = EndType.NoEnd;
            this.Instances = 10;
        }

        public void FromRule(RecurrenceRule recurrence, SPRegionalSettings regionalSettings)
        {
            if (recurrence.HasWindowEnd)
            {
                this.Type = EndType.ByDate;
                this.Date = regionalSettings.TimeZone.UTCToLocalTime(recurrence.WindowEnd);
            }
            else if (recurrence.RepeatInstances > 0)
            {
                this.Type = EndType.ByInstances;
                this.Instances = recurrence.RepeatInstances;
            }
            else
                this.Type = EndType.NoEnd;
        }
    }
}
